package com.xuhuan.demo.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: 徐欢
 * @date: 2021/7/28 14:11
 * @description: 文件工具类
 */
@Slf4j
public class FileUtilDemo {

    @Test
    public void testPrintDirFileName() {
        File dir = new File("E:\\3.project_svn\\fia_aims_bxzg_zsxr\\trunk\\doc\\22.清库脚本\\项目自定义表");

        File[] files = dir.listFiles();
        String fileNames = Arrays.stream(files)
                .map(file -> {
                    String fullFileName = file.getName();
                    return fullFileName.split("\\.")[0];
                }).collect(Collectors.joining("','"));
        log.info(fileNames);
    }

    @Test
    public void testDistinctFileContent() {
        File productSqlFile = new File("E:\\3.project_svn\\fia_aims_bxzg_zsxr\\trunk\\doc\\22.清库脚本\\徐欢整理版\\1.1产品表.sql");
        Set<String> productSet = FileUtil.readTextToSet(productSqlFile);
        productSet.stream().forEach(s -> System.out.println(s));

        File projectSqlFile = new File("E:\\3.project_svn\\fia_aims_bxzg_zsxr\\trunk\\doc\\22.清库脚本\\徐欢整理版\\1.2项目自定义表.sql");
        Set<String> projectSet = FileUtil.readTextToSet(projectSqlFile);

    }

    @Test
    public void testGetDirFileSet() {
        File beforeDir = new File("D:\\20.项目管理\\7.待办\\1.未完成\\20220421_struts2版本升级\\光大OA\\1.升级前lib");
        Set<String> beforeSetList = new HashSet<>();
        FileUtil.getDirFileSet(beforeDir, beforeSetList);

        File afterDir = new File("D:\\20.项目管理\\7.待办\\1.未完成\\20220421_struts2版本升级\\光大OA\\2.升级后lib");
        Set<String> afterSetList = new HashSet<>();
        FileUtil.getDirFileSet(afterDir, afterSetList);

        //删除的jar
        System.out.println("-----删除的jar-----");
        Collection<String> deleteList = CollectionUtils.subtract(beforeSetList, afterSetList);
        deleteList.stream().forEach(name -> {
            System.out.println(name);
        });
        //新增的jar
        System.out.println("-----新增的jar-----");
        Collection<String> addList = CollectionUtils.subtract(afterSetList, beforeSetList);
        addList.stream().forEach(name -> {
            System.out.println(name);
        });
    }


    /**
     * 测试合并多个txt为同一个文件
     */
    @Test
    public void testMergeCodeTxt() {
        File dir = new File("D:\\20.项目管理\\0.各个项目文件\\国寿投\\2.生产问题-问题集中处理\\部署记录\\20220606\\2");
        File resultFile = new File("D:\\20.项目管理\\0.各个项目文件\\国寿投\\2.生产问题-问题集中处理\\部署记录\\20220606\\3\\code.txt");
        FileUtil.mergeCodeTxt(dir, null, resultFile);
    }

    /**
     * 测试汇总部署说明
     */
    @Test
    public void testMergeDeployDoc() {
        File dir = new File("D:\\20.项目管理\\0.各个项目文件\\国寿投\\2.生产问题-问题集中处理\\部署记录\\20220608\\1");
        File resultDir = new File("D:\\20.项目管理\\0.各个项目文件\\国寿投\\2.生产问题-问题集中处理\\部署记录\\20220608\\2");
        String[] ignoreDir = {
                "20220521_合并发布包18：46"
                , "20220522_合并发布包19：40"
        };
        FileUtil.mergeDeployDoc(dir, ignoreDir, resultDir, 1);
    }

}
